package com.bing.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bing.my.entity.Goose;
import com.bing.my.entity.Member;
import com.bing.my.mapper.GooseMapper;
import com.bing.my.service.GooseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品数据 服务实现类
 * </p>
 *
 * @author bing
 * @since 2021-10-04
 */
@Service
public class GooseServiceImpl extends ServiceImpl<GooseMapper, Goose> implements GooseService {

    @Resource
    private GooseMapper gooseMapper;

    @Override
    public int insert(Goose goose) {
        return gooseMapper.insert(goose);
    }

    @Override
    public int deleteById(String id) {
        return gooseMapper.deleteById(id);
    }

    @Override
    public int update(Goose goose) {
        return gooseMapper.updateById(goose);
    }

    @Override
    public Page<Goose> selectPage(String search,
                                  String select,
                                  Integer pageNum,
                                  Integer pageSize) {
        LambdaQueryWrapper<Goose> like = null;
        if("price".equals(select)){
            // 根据价格查询
             like = Wrappers.<Goose>lambdaQuery().eq(Goose::getPrice, search);
        }else if("cost".equals(select)){
            // 根据成本查询
            like = Wrappers.<Goose>lambdaQuery().eq(Goose::getCost, search);
        }else if("gooseID".equals(select)){
            // 根据商品ID查询
            like = Wrappers.<Goose>lambdaQuery().like(Goose::getGooseId, search);
        }else {
            // 根据商品名称查询
            like = Wrappers.<Goose>lambdaQuery().like(Goose::getGooseName, search)
                    // 根据创建时间倒序
                    .orderByDesc(Goose::getGmtCreate);
        }
        Page<Goose> goosePage = gooseMapper.selectPage(new Page<>(pageNum, pageSize), like);

        return goosePage;
    }

}
